<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/stores'
import { userUpdataService } from '@/api/user'
// const userStore = useUserStore()
const {
  user: { id, username, nickname, email },
  getUserInfo
} = useUserStore()
const userInfo = ref({
  id,
  username,
  nickname,
  email
})
const formRef = ref()
const rules = {
  nickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { min: 2, max: 15, message: '昵称长度在 2 到 15 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] }
  ]
}

const submitForm = async () => {
  // 表单提交逻辑
  await formRef.value.validate()
  const res = await userUpdataService(userInfo.value)
  await getUserInfo()
  ElMessage.success(res.data.msg)
}
</script>
<template>
  <page-container title="基本资料">
    <el-row>
      <el-col :span="12">
        <el-form ref="formRef" label-width="100px" size="large" :model="userInfo" label-position="top" :rules="rules">
          <el-form-item label="登录名称">
            <el-input v-model="userInfo.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="用户昵称" prop="nickname">
            <el-input v-model="userInfo.nickname"></el-input>
          </el-form-item>
          <el-form-item label="用户邮箱" prop="email">
            <el-input v-model="userInfo.email"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">提交修改</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </page-container>
</template>
<style scoped></style>
